<?php
namespace app\mobilenode\model;

use app\BaseModel;
use think\facade\Db;

class RoleList extends BaseModel
{
    //定义表名
    protected $name = 'massage_mobile_role_list';



    /**
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(isset($data['node'])){

            $node = $data['node'];

            unset($data['node']);
        }

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        if(isset($node)){

            $this->updateSome($node,$id,$data['uniacid']);
        }

        return $id;

    }



    /**
     * @功能说明:添加权限节点
     */
    public function updateSome($data,$id,$uniacid){

        $node_model = new RoleNode();

        $node_model->where(['role_id'=>$id])->delete();

        if(!empty($data)){

            foreach ($data as $k=>$v){

                $data[$k]['uniacid'] = $uniacid;

                $data[$k]['role_id'] = $id;

                $data[$k]['auth']    = !empty($v['auth'])?implode(',',$v['auth']):'';

            }

            $node_model->saveAll($data);

        }

        return true;
    }



    /**
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['node'])){

            $node = $data['node'];

            unset($data['node']);
        }

        $res = $this->where($dis)->update($data);

        if(isset($node)){

            $this->updateSome($node,$dis['id'],$data['uniacid']);

        }

        return $res;

    }


    /**
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }








}